package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw.activity.CommonWebActivity;
import com.shwread.android.qysw10000136.R;
import com.shwread.android.ui.dialog.GuideDialog;
import com.shwread.android.ui.dialog.NetworkDialog;
import com.shwread.android.ui.dialog.QRcodeDownloadDialog;
import com.shwread.android.ui.dialog.VersionDialog;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.http.face.QryNewVersionAction;
import com.tgx.tina.android.ipc.framework.IUIPage;

import java.util.ArrayList;

import base.tina.core.task.infc.ITaskResult;
import logic.bean.AppInfo;
import logic.constants.AppConfig;
import logic.constants.DefaultConsts;
import logic.util.AppUtils;
import logic.util.NetworkUtil;
import logic.util.Util;

/**
 * @Project： Lqyd_Android
 * @Title：AboutPage
 * @Description：关于界面
 * @Author：wayne
 * @Date：2014年8月19日下午8:35:18
 */
public class AboutPage
        extends
        AbstractUIPage<BaseBusinessActivity>
        implements
        OnClickListener
{
	
	private RelativeLayout    rlayHelp,rlayfeedback, rlayChechUpdate, rlayCopyRight, rlayQRcode,rlayService;
	private TextView          tvTitle, tvVersion;
	private Button            btnBack;
	private static AboutPage  instance;
	
	private ArrayList<String> choiceItems = new ArrayList<String>();
	private PopupWindow       choicePopupWindow;
	private ListAdapter       mAdapter;
	private ListView          listView;
	
	private VersionDialog     mDialog;
	private ImageView         img;
	
	public static AboutPage getInstance(BaseBusinessActivity t) {
		/*if (null == instance) */instance = new AboutPage(t);
		return instance;
	}
	
	public AboutPage(BaseBusinessActivity t) {
		super(t);
	}
	
	@Override
	public <E extends Activity> View createView(E arg0, int arg1) {
		if (null != curMyView) return curMyView;
		
		curMyView = bActivity.getLayoutInflater().inflate(R.layout.user_info_setting_about, null);
		
		initView();
		setLinstener();
		
		return curMyView;
	}
	
	private void setLinstener() {
		btnBack.setOnClickListener(this);
		rlayHelp.setOnClickListener(this);
		rlayfeedback.setOnClickListener(this);
		rlayChechUpdate.setOnClickListener(this);
		rlayCopyRight.setOnClickListener(this);
		rlayQRcode.setOnClickListener(this);
		rlayService.setOnClickListener(this);
		tvVersion.setOnLongClickListener(new OnLongClickListener()
		{
			
			@Override
			public boolean onLongClick(View v) {
				queryVersionCode();
				return false;
			}
		});
	}
	
	private void initView() {
		tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
		tvTitle.setText("关于");
		btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		tvVersion = (TextView) curMyView.findViewById(R.id.user_info_setting_about_version);
		tvVersion.setText("v" + AppUtils.getVersionName(bActivity));
		
		rlayHelp = (RelativeLayout) curMyView.findViewById(R.id.user_info_setting_about_help_rlay);
		rlayChechUpdate = (RelativeLayout) curMyView.findViewById(R.id.user_info_setting_about_check_update_rlay);
		rlayfeedback = (RelativeLayout) curMyView.findViewById(R.id.user_info_setting_about_feedback_rlay);
		rlayCopyRight = (RelativeLayout) curMyView.findViewById(R.id.user_info_setting_about_copyright_rlay);
		rlayQRcode = (RelativeLayout) curMyView.findViewById(R.id.user_info_setting_about_qrcode_rlay);
		rlayService = (RelativeLayout) curMyView.findViewById(R.id.user_info_setting_about_service_rlay);
		
		choiceItems.add("内部版本号： " + AppConfig.getVersionCode(bActivity));
		choiceItems.add("SVN版本号： " + AppConfig.getSvnVersion());
		choiceItems.add("版本  标识：" + AppConfig.getEnterpriseId());
		choiceItems.add("服务器地址：" + AppConfig.getServerPath());
	}
	
	/**
	 * 查看内部版本号和svn号
	 */
	private void queryVersionCode() {
		
		listView = new ListView(context);
		listView.setFocusable(true);
		listView.setFocusableInTouchMode(true);
		listView.setDivider(new ColorDrawable(Color.GRAY));
		listView.setSelector(android.R.drawable.list_selector_background);
		listView.setCacheColorHint(Color.TRANSPARENT);
		listView.setDividerHeight(1);
		mAdapter = new ArrayAdapter<String>(context, R.layout.simple_list_item, choiceItems);
		listView.setAdapter(mAdapter);
		choicePopupWindow = new PopupWindow(listView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
		choicePopupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.simple_popup_window_bg));
		choicePopupWindow.setOutsideTouchable(true);// 设置pWindow之外的布局可点击
		choicePopupWindow.setTouchInterceptor(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
				{
					choicePopupWindow.dismiss();
					return true;
				}
				return false;
			}
		});
		choicePopupWindow.showAtLocation(curMyView, Gravity.CENTER, 0, 0);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				choicePopupWindow.dismiss();
			}
		});
	}
	
	@Override
	public int enter(IUIPage<?> arg0) {
		return 0;
	}
	
	@Override
	public int leave(IUIPage<?> arg0) {
		return 0;
	}
	
	@Override
	public void notifyView(int arg0, Bundle arg1) {
		
	}
	
	@Override
	public void setStatus(int arg0) {
		
	}
	
	@Override
	public View updateView(int arg0, ITaskResult arg1) {
		return null;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				bActivity.onBackPressed();
				break;
			case R.id.user_info_setting_about_help_rlay:
				Intent intent = new Intent(bActivity,CommonWebActivity.class);
				intent.putExtra(DefaultConsts.title_s, bActivity.getResources().getString(R.string.about_item_help));
				intent.putExtra(DefaultConsts.req_url_s, DefaultConsts.ABOUT_HELP_URL);
				bActivity.startActivity(intent);
				break;
			case R.id.user_info_setting_about_feedback_rlay:
				bActivity.changePage(new FeedBackPage(bActivity), DEFAULT_STATUS);
				break;
			case R.id.user_info_setting_about_check_update_rlay:
				clickCheckUpdate();
				break;
			case R.id.user_info_setting_about_copyright_rlay:
				bActivity.changePage(new CopyRightPage(bActivity), DEFAULT_STATUS);
				break;
			case R.id.user_info_setting_about_qrcode_rlay:
				QRcodeDownloadDialog dialog = new QRcodeDownloadDialog(bActivity);
				dialog.show();
				break;
			case R.id.user_info_setting_about_service_rlay:
//				AboutServiceDialog dialog2 = new AboutServiceDialog(bActivity);
//				dialog2.show();
				 GuideDialog dialog2 = new GuideDialog(bActivity, R.layout.about_service_dialog);
				 dialog2.show();
				break;
			
			default:
				break;
		}
	}
	
	void clickCheckUpdate() {
		if (!NetworkUtil.isNetAvailable(bActivity))
		{
			new NetworkDialog(bActivity).show();
			return;
		}
		else
		{
			View view = LayoutInflater.from(bActivity).inflate(R.layout.common_loading, null);
			img = (ImageView) view.findViewById(R.id.common_loading__img);
			img.setBackgroundResource(R.drawable.round_progress);
			AnimationDrawable drawable = (AnimationDrawable) img.getBackground();
			drawable.start();
			final PopupWindow moreAccountPup = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			moreAccountPup.setFocusable(false);
			moreAccountPup.showAtLocation(curMyView, Gravity.CENTER, 0, 0);
			mDialog = new VersionDialog(bActivity);
			new QryNewVersionAction(bActivity, AppConfig.getVersionCode(bActivity), new ActionListener()
			{
				
				@Override
				public void OK(final Object obj) {
					bActivity.getUIHandler().post(new Runnable()
					{
						
						@Override
						public void run() {
							AppInfo info = (AppInfo) obj;
							moreAccountPup.dismiss();
							if (null != info)
							{
								if (AppConfig.getVersionCode(bActivity) >= info.versionCode2 || !info.isHasNewVersion() || !info.isReleased())
								{
									Util.showToast(bActivity, "您使用的已是最新版本，谢谢使用");
								}
								else
								{
									if (info.isForce()) {//TODO 需要修改
									Util.showToast(bActivity, "您使用的已是最新版本，谢谢使用");
										return;
									}
									
									mDialog.setVersionInfo((AppInfo) obj);
									mDialog.show();
								}
							}
							else
							{
								Util.showToast(bActivity, "您使用的已是最新版本，谢谢使用");
							}
						}
					});
				}
				
				@Override
				public void HTTPERROR(int statusCode) {
					showToast("请检查网络", moreAccountPup);
				}
				
				@Override
				public void ERROR(int resultCode, String message) {
					showToast("您使用的已是最新版本，谢谢使用", moreAccountPup);
				}
			}).start();
		}
		
	}
	
	void showToast(final String message, final PopupWindow pop) {
		bActivity.getUIHandler().post(new Runnable()
		{
			
			@Override
			public void run() {
				pop.dismiss();
				Util.showToast(bActivity, message);
			}
		});
	}
	
}
